package com.scommunity.admin.controller;

import com.github.pagehelper.PageInfo;
import com.scommunity.admin.vo.BuildingQO;
import com.scommunity.common.ResponseResult;
import com.scommunity.domain.Building;
import com.scommunity.service.IBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/building")
public class BuildingController {
    @Autowired
    IBuildingService buildingService;

    @GetMapping({"/list/{pageNum}"})
    public Object list(BuildingQO buildingQO, @PathVariable(required = false) Integer pageNum) {
        if (pageNum == null) {
            pageNum = 1;
        }
        PageInfo<Building> pageInfo = buildingService.findPageInfoByCondition(buildingQO, pageNum, 5);
        return ResponseResult.success(pageInfo);
    }

    @GetMapping("/getAll")
    public Object getAll() {
        List<Building> buildings = buildingService.getAll();
        return ResponseResult.success(buildings);
    }

    @GetMapping("/getAllByStatus")
    public Object getAllByStatus() {
        List<Building> buildings = buildingService.getAllByStatus();
        return ResponseResult.success(buildings);
    }

    @PostMapping("/add")
    public Object add(@RequestBody Building building) {
        buildingService.add(building);
        return ResponseResult.success();
    }

    @PostMapping("/update")
    public Object update(@RequestBody Building building) {
        buildingService.update(building);
        return ResponseResult.success();
    }

    @GetMapping("/del")
    public Object del(String id) {
        if (buildingService.getStatuseById(id) == 0) {
            buildingService.del(id);
            return ResponseResult.success();
        } else {
            return ResponseResult.error(201, "楼栋已开放，不能删除");
        }
    }

}
